package com.ruoyi.web.utils.email;

import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Slf4j
public class MailService {

    private static final String FROM = "wuxinxin0604@163.com";

    @Resource
    private JavaMailSender jms;

    /**
     * @desc  发送邮件 多个
     * @author fwx 2024/2/23 13:59
     **/
    public void sendSimpleMail(String to, String subject, String content) {

        if(StringUtils.isBlank(to)){
            return;
        }

        String[] emails = to.split(",");

        for (String email : emails) {
            try {
                SimpleMailMessage message = new SimpleMailMessage();
                message.setFrom(FROM);
                message.setTo(email);
                message.setSubject(subject);
                message.setText(content);
                jms.send(message);
            } catch (MailException e) {
                log.info("邮箱[{}]发送失败",email);
                log.info(e.getMessage());
            }
        }
    }

}
